package com.ityedao.Stream流.获取流的方式;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Test {
    public static void main(String[] args) {
        // 1、如何获取List集合的Stream流？
        List<String> names = new ArrayList<>();
        Collections.addAll(names, "张三丰", "张无忌", "周芷若", "赵敏", "张蔷");
        Stream<String> stream = names.stream();

        // 2、如何获取Set集合的Stream流？
        Set<String> set = new HashSet<>();
        Collections.addAll(set, "刘德华", "张曼玉", "蜘蛛精", "德海", "德玛西亚");
        set.stream().filter(s -> s.contains("德")).forEach(s -> System.out.println(s));


        // 3、如何获取Map集合的Stream流
        Map<String, Double> map = new HashMap<>();
        map.put("古力娜扎", 172.3);
        map.put("迪丽热巴", 168.3);
        map.put("马尔扎哈", 166.5);
        map.put("卡尔扎巴", 168.4);

        Set<String> keys = map.keySet();
        Stream<String> ks = keys.stream(); // 拿到了键的流

        Collection<Double> values = map.values();
        Stream<Double> vs = values.stream(); // 拿到了值的流
        // 可以将键值对分开进行留的操作
        // 但更常用的是map里的键值对当成整体进行留的相关操作：
        Set<Map.Entry<String, Double>> entries = map.entrySet();
        Stream<Map.Entry<String, Double>> kvs = entries.stream();
        kvs.filter(s -> s.getKey().contains("巴")).forEach(s -> System.out.println(s.getKey() + "-->" + s.getValue()));

        // 可以简写
        map.entrySet().stream().filter(s -> s.getValue() > 170.0).collect(Collectors.toList()).forEach(s -> System.out.println(s));

        // 4、如何获取数组的Stream流？
        String[] names2 = {"张翠山", "东方不败", "唐大山", "独孤求败", "东方不败"};
        // ①
        Stream<String> s1 = Arrays.stream(names2);
        // ②
        Stream<String> s2 = Stream.of(names2);
    }
}
